<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/15
 * Time: 11:41
 */

namespace app\home\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * @title 统计中心
 * @description 接口说明
 * @group 后台管理
 */
class Statistics extends Backend
{
    protected $dept = ['前端', '后端', '产品经理', '测试', '运维','管理员'];

    /**
     * @title 获取工时统计数据
     * @description 接口说明
     * @author jyh
     * @url /home/statistics/getStatLst
     * @method POST
     */
    public function getStatLst()
    {
        if ( $this->request->isPost() ) {

            $map = $this->request->post();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            $needModel = new \app\home\model\ProjectNeed();
            // 判断是否有条件
            // 有$map条件 就是筛选
            $where = [];
            $like = '';
            foreach ($map as $key => $value) {
                if ($value === '') unset($map[$key]);
            }
            $where['pn.status'] = ['<>',0];
            $where['rp.status'] = 1;
            $where['rp.newPlan'] = 1;
            if (isset($map['user_id'])) {
                $where['rp.user_id'] = $map['user_id'];
            }

            if (isset($map['time'])) {
                $where['rp.ctime'][] = ['>=',strtotime(date('Y-m-d',$map['time'][0]) . ' 00:00:00')];
                $where['rp.ctime'][] = ['<=',strtotime(date('Y-m-d',$map['time'][1]) . ' 23:59:59')];
            } else {
                $where['rp.ctime'][] = ['>=',strtotime(date('Y-m-d',strtotime('-6 day')) . ' 00:00:00')];
                $where['rp.ctime'][] = ['<=',strtotime(date('Y-m-d',time()) . ' 23:59:59')];
            }

            if (isset($map['project_id'])) {
                $where['pn.project_id'] = $map['project_id'];
            }

            if (isset($map['product_id'])) {
                $where['pn.product_id'] = $map['product_id'];
                if (isset($map['versions_id'])) {
                    $where['pn.versions_id'] = $map['versions_id'];
                }
            }

            if (isset($map['status'])) {
                $where['pn.status'] = ['in',$map['status']];
            }

            if (isset($map['keywords'])) {
                $like = "pn.name like '%{$map['keywords']}%' OR u.name like '%{$map['keywords']}%' OR p.name like '%{$map['keywords']}%' OR pp.name like '%{$map['keywords']}%'";
                switch ($map['keywords']) {
                    case '前端';
                        $deptid = 0;
                        break;
                    case '后端';
                        $deptid = 1;
                        break;
                    case '产品经理';
                        $deptid = 2;
                        break;
                    case '测试';
                        $deptid = 3;
                        break;
                    case '运维';
                        $deptid = 4;
                        break;
                    case '管理员';
                        $deptid = 5;
                        break;
                }
                if (isset($deptid)) $like .= " OR u.deptid like '%{$deptid}%'";
            }
            $needStatus = ['筹备中', '待开发', '开发中', '待测试', '测试中', '待上线', '已上线'];

            $data = Db::name('project_need')
                ->alias('pn')
                ->join('RecordPlan rp','pn.`id` = rp.`need_id` and rp.status=1','left')
                ->join('User u','u.`id` = rp.`user_id`','left')
                ->where($where)
                ->where('pn.deleted','null')
                ->where($like)
                ->field("CASE
                WHEN pn.`front_user` = rp.`user_id` THEN
                 FORMAT(SUM(CASE pn.`front_adjust` WHEN '0.0' THEN pn.`front_predict` ELSE pn.`front_adjust` END * pn.`front_plan`/100),1)
                WHEN pn.`after_user` = rp.`user_id` THEN
                 FORMAT(SUM(CASE pn.`after_adjust` WHEN '0.0' THEN pn.`after_predict` ELSE pn.`after_adjust` END * pn.`after_plan`/100),1)
                ELSE
                 0
                END AS `manhour`,pn.`id`,rp.`user_id`,u.`name` as `uname`,pn.name,pn.`project_id`,u.`deptid`,pn.`status`,pn.`type`,pn.`product_id`,pn.`versions_id`,rp.`ctime` ptime,pn.`status`,pn.`block_id`")
                ->group('rp.`ctime`')
                ->order('pn.ptime','desc')//->select(false);print_r($data);die;
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false)->toArray();

            $projectName = Db::name('project')->column('name','id');
            $block = Db::name('block')->where('deleted',null)->column('name','id');
            $versions = Db::name('versions')->where('deleted',null)->column('name','id');
            $deploy = Db::name('deploy')->where('deleted',null)->column('name','id');

            $needData = [];
            $status = array_count_values(array_column($data['data'],'status'));
            foreach ($needStatus as $key => $value) {
                $needData[$key]['value'] = isset($status[$value]) ? $status[$value] : 0;
                $needData[$key]['name'] = $needStatus[$key];
            }
            foreach ($data['data'] as &$value) {
                $value['deptid'] = $this->dept[$value['deptid']];
                $value['status'] = $needStatus[$value['status']];
                $value['pname'] = $projectName[$value['project_id']];
                $value['product_id'] = $projectName[$value['product_id']];
                $value['vname'] = $versions[$value['versions_id']];
                $value['block_id'] = $block[$value['block_id']];
                $value['type'] = $deploy[$value['type']];
                $value['ptime'] = $value['ptime'] ? date('Y-m-d',$value['ptime']) : '';
            }
            // 总时浮点转为字符串
            $hourSum = (string) array_sum(array_column($data['data'],'manhour'));
            return json(['code' => 0, 'msg' => 'success', 'data' => $data,'needStat' => $needData,'hourSum' => $hourSum]);
        }
    }


    public function getBugLst()
    {
        if ( $this->request->isPost() ) {

            $map = $this->request->post();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            $bugModel = new \app\home\model\ProjectBug();
            // 判断是否有条件
            // 有$map条件 就是筛选
            $where = [];
            $like = '';
            foreach ($map as $key => $value) {
                if ($value === '') unset($map[$key]);
            }
            if (isset($map['user_id'])) $where['pb.assigner'] = $map['user_id'];
            if (isset($map['time'])) {
                $where['pb.ctime'][] = ['>=',strtotime(date('Y-m-d',$map['time'][0]) . ' 00:00:00')];
                $where['pb.ctime'][] = ['<=',strtotime(date('Y-m-d',$map['time'][1]) . ' 23:59:59')];
            } else {
                $where['pb.ctime'][] = ['>=',strtotime(date('Y-m-d',strtotime('-6 day')) . ' 00:00:00')];
                $where['pb.ctime'][] = ['<=',strtotime(date('Y-m-d',time()) . ' 23:59:59')];
            }
            if (isset($map['project_id'])) $where['pb.project_id'] = $map['project_id'];
            if (isset($map['product_id'])) {
                $where['pb.product_id'] = $map['product_id'];
                if (isset($map['versions_id'])) $where['pb.versions_id'] = $map['versions_id'];
            }
            if (isset($map['status'])) $where['pb.status'] = ['in',$map['status']];
            if (isset($map['degree'])) $where['pb.degree'] = $map['degree'];
            if (isset($map['keywords'])) {
                $like = "pb.title like '%{$map['keywords']}%' OR u.name like '%{$map['keywords']}%' OR p.name like '%{$map['keywords']}%' OR pp.name like '%{$map['keywords']}%'";
                switch ($map['keywords']) {
                    case '前端';
                        $deptid = 0;
                        break;
                    case '后端';
                        $deptid = 1;
                        break;
                    case '产品经理';
                        $deptid = 2;
                        break;
                    case '测试';
                        $deptid = 3;
                        break;
                    case '运维';
                        $deptid = 4;
                        break;
                    case '管理员';
                        $deptid = 5;
                        break;
                }
                if (isset($deptid)) $like .= " OR u.deptid like '%{$deptid}%'";

            }

            $needStatus = ['未确认', '已确认', '已解决', '已关闭', '激活'];
            $data = Db::name('project_bug')
                ->alias('pb')
                ->join('User u','u.`id` = pb.`assigner`','left')
                ->where($where)
                ->where('pb.deleted','null')
                ->where($like)
                ->field("pb.`id`,u.`name` as `uname`,pb.`title`,u.`deptid`,pb.`status`,pb.`degree`,pb.`type`,pb.`creator`,pb.`project_id`,pb.product_id,pb.`versions_id`,pb.`ctime`,pb.`status`")
                ->group('pb.`ctime`')
                ->order('pb.ctime','desc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false)->toArray();


            $projectName = Db::name('project')->column('name','id');
            $user = Db::name('User')->column('name','id');
            $versions = Db::name('versions')->where('deleted',null)->column('name','id');
            $deploy = Db::name('deploy')->where('deleted',null)->column('name','id');

            $bugData = [];
            $status = array_count_values(array_column($data['data'],'status'));
            foreach ($needStatus as $key => $value) {
                $bugData[$key]['value'] = isset($status[$value]) ? $status[$value] : 0;
                $bugData[$key]['name'] = $needStatus[$key];
            }
            foreach ($data['data'] as &$val) {
                $val['deptid'] = $this->dept[$val['deptid']];
                $val['creator'] = $user[$val['creator']];
                $val['status'] = $needStatus[$val['status']];
                $val['pname'] = $projectName[$val['project_id']];
                $val['product_id'] = $projectName[$val['product_id']];
                $val['vname'] = $versions[$val['versions_id']];
                $val['type'] = $deploy[$val['type']];
                $val['ctime'] = $val['ctime'] ? date('Y-m-d',$val['ctime']) : '';
            }
            return json(['code' => 0, 'msg' => 'success', 'data' => $data,'needStat' => $bugData,'hourSum' => array_sum(array_column($data['data'],'manhour'))]);
        }
    }
}